package com.cjl.algorithm;


import com.cjl.algorithm.base.MySort;
import com.cjl.algorithm.simple.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {

        LRU<String,String>  lru = new LRU<>(3);
        lru.put("a","a");
        lru.put("b","b");
        lru.put("c","c");
        lru.put("b","b");
        lru.put("e","e");
        lru.print();

//
//        MySort bubbleSort = new BubbleSort();
//        bubbleSort.myprint(bubbleSort.sort(new int[]{99, 1, 1, 2, 5, 2, 3, 32, 3, 4, 1, 5, 2, 1}));
//
//        MySort selectionSort = new SelectionSort();
//        selectionSort.myprint(selectionSort.sort(new int[]{99, 1, 1, 2, 5, 2, 3, 32, 3, 4, 1, 5, 2, 1}));
//
//        MySort insertionSort = new InsertionSort();
//        insertionSort.myprint(insertionSort.sort(new int[]{99, 1, 1, 2, 5, 2, 3, 32, 3, 4, 1, 5, 2, 1}));
//
//
//        int initialCapacity = 2;
//        long time = System.currentTimeMillis();
//        int capacity = 1;
//        while (capacity < initialCapacity)
//            capacity <<= 1;
//
//        System.out.println("Test.main  ---" + (System.currentTimeMillis() - time));
//        System.out.println("Test.main capacity " + capacity);
//
//        time = System.currentTimeMillis();
//        int n = initialCapacity - 1;
//        n |= n >>> 1;
//        n |= n >>> 2;
//        n |= n >>> 4;
//        n |= n >>> 8;
//        n |= n >>> 16;
//        System.out.println("Test.main  ---" + (System.currentTimeMillis() - time));
//        System.out.println("Test.main  n+1 " + (n + 1));
//
//        LinkedList<Integer> list = new LinkedList<>();
//
//
//        list.addLast(1);
//        list.addLast(2);
//
//        System.out.println("Test.main list " + list);
//
//
//        int[] res = maxSlidingWindow(new int[]{3, 1, 4, 5, 2, 7}, 3);
//        for (int re : res) {
//            System.out.println("Test.main ---- " + re);
//        }
//
//
//        System.out.println("Test.main "+maxVowels("abciiidef",3));
//        System.out.println("Test.main "+maxVowels("aeiou",2));
//
//
//        System.out.println("Test.main "+maxVowels("leetcode",3));
//        System.out.println("Test.main "+maxVowels("rhythms",4));

    }

    //结果数组确认   length-k+1
    //k窗口内  最大，链表循环
    //移动下一个时  排除最外层最大
    public static int[] maxSlidingWindow(int[] nums, int k) {
        int right = 0;
        int index = 0;
        int[] res = new int[nums.length - k + 1];
        LinkedList<Integer> list = new LinkedList<>();

        while (right < nums.length) {
            while (!list.isEmpty() && nums[right] > list.peekLast()) {
                list.removeLast();
            }
            list.addLast(nums[right]);
            right++;
            if (right >= k) {
                res[index++] = list.peekFirst();
                if (list.peekFirst() == nums[right - k]) {
                    list.removeFirst();
                }
            }
        }
        return res;
    }

    public static int maxVowels(String s, int k) {
        int res = 0;
        int length = s.length();
        int right = k;
        String vowels = "aeiou";
        while (right < length){
            String kString = s.substring(right-k,right);
            int count = 0;
            for (int i = 0; i < kString.length(); i++) {
                char c = kString.charAt(i);
                if (vowels.contains(String.valueOf(c))){
                    count++;
                }
            }
            res = Math.max(count,res);

            right++;

        }

        return res;
    }


    public static int maxVowels2(String s, int k) {
        int max = 0;
        int right = 0;
        int sum = 0;
        while (right < s.length()){
            sum += isYuan(s.charAt(right));
            right++;
            if (right >= k){
                max = Math.max(max,sum);
                sum -= isYuan(s.charAt(right-k));
            }
        }
        return max;
    }

    public static int isYuan(char s) {
        return s=='a' || s=='e' ||s=='i' ||s=='o' ||s=='u' ? 1:0;
    }
}